import tkinter as tk
from tkinter import filedialog, ttk
import temu_action,temu_table,add_exif,combine_image

def select_folder(entry):
    """选择文件夹并填入输入框"""
    folder_selected = filedialog.askdirectory()
    if folder_selected:
        entry.delete(0, tk.END)
        entry.insert(0, folder_selected)

# 独立的执行函数
def execute_png_to_jpg(path):
    temu_action.convert_png_to_jpg(path)

def execute_add_exif(path):
    add_exif.apply_exif_to_folder(path)

def execute_generate_sku(path):
    combine_image.pic_operate(path)

def execute_temu_kuaimai(path, category):
    temu_action.main(path, category)

def execute_temu_table(path, category):
    temu_table.main(path, category)

root = tk.Tk()
root.title("小成 - 运营专用")
root.iconbitmap("logo.ico")
root.geometry("760x340")
root.resizable(False, False)  # 固定窗口大小


# 统一按钮样式
btn_style = {"bg": "#2196F3", "fg": "white", "bd": 0, "padx": 10, "height": 1, "pady": 2, "width": 6,"font": ("Arial", 10, "bold")}

# 统一创建框架（保证每个执行函数独立）
def create_section(title, execute_func):
    frame = tk.Frame(root, relief=tk.GROOVE, bd=2)
    frame.pack(fill="x", padx=5, pady=3)

    tk.Label(frame, text=title, font=("Arial", 10, "bold")).pack(anchor="w", padx=5, pady=3)

    inner_frame = tk.Frame(frame)
    inner_frame.pack(fill="x", padx=5, pady=3)

    tk.Label(inner_frame, text="文件夹：", font=("Arial", 10)).grid(row=0, column=0, padx=5, pady=3, sticky="w")
    
    entry = tk.Entry(inner_frame, width=70, font=("Arial", 10))
    entry.grid(row=0, column=1, padx=5, pady=3, ipady=4, sticky="ew")

    tk.Button(inner_frame, text="选择", command=lambda: select_folder(entry), **btn_style).grid(row=0, column=2, padx=5, pady=3)
    tk.Button(inner_frame, text="执行", command=lambda: execute_func(entry.get()), **btn_style).grid(row=0, column=3, padx=5, pady=3)

# PNG 转 JPG
create_section("PNG转JPG[size<2M]", execute_png_to_jpg)

# JPG/PNG 添加 EXIF 备注
create_section("JPG/PNG 添加EXIF备注", execute_add_exif)

# 款号图快速生成（无单文件按钮）
create_section("款号图快速合成", execute_generate_sku)

# TEMU 批量上款（特殊按钮 + 选择框）
frame_temu = tk.Frame(root, relief=tk.GROOVE, bd=2)
frame_temu.pack(fill="x", padx=5, pady=3)

tk.Label(frame_temu, text="temu批上款", font=("Arial", 10, "bold")).pack(anchor="w", padx=5, pady=3)

inner_temu = tk.Frame(frame_temu)
inner_temu.pack(fill="x", padx=5, pady=3)

tk.Label(inner_temu, text="文件夹：", font=("Arial", 10)).grid(row=0, column=0, padx=5, pady=3, sticky="w")

entry_temu = tk.Entry(inner_temu, width=42, font=("Arial", 10))
entry_temu.grid(row=0, column=1, padx=5, pady=3, ipady=5, sticky="ew")

# 选择框选项
temu_categories = ["TEMU男装", "TEMU女装", "TEMU大童", "TEMU小童", "TEMU亚洲女装"]
category_var = tk.StringVar(root)
category_var.set(temu_categories[0])  # 默认值

# 选择框（使其风格与输入框匹配）
dropdown = ttk.Combobox(inner_temu, textvariable=category_var, values=temu_categories, font=("Arial", 10), state="readonly")
dropdown.grid(row=0, column=2, padx=5, pady=3,ipady=3, sticky="ew")
dropdown.config(width=12)  # 选择框宽度优化

tk.Button(inner_temu, text="选择", command=lambda: select_folder(entry_temu), **btn_style).grid(row=0, column=3, padx=5, pady=3)
tk.Button(inner_temu, text="图包生成", command=lambda: execute_temu_kuaimai(entry_temu.get(), category_var.get()), **btn_style).grid(row=0, column=4, padx=5, pady=3)
tk.Button(inner_temu, text="表格制作", command=lambda: execute_temu_table(entry_temu.get(), category_var.get()), **btn_style).grid(row=0, column=5, padx=5, pady=3)

root.mainloop()